package com.skcc.ts.nsi.expense;

import java.util.ArrayList;

import com.skcc.ts.nsi.R;
import com.skcc.ts.nsi.service.MEDataHandler;
import com.skcc.ts.nsi.service.SocketClientService;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class MEExpenseListUpdate extends Activity {
	private ArrayList<String[]> pjtList = null;
	private ArrayList<String[]> acntList = null;
	private TextView exp_date;
	private TextView exp_money;
	private Button btn_expense_update;
	private String pjtCode;
	private String acntCode;

	private String selectedPjt;
	private String selectedAcnt;
	private String myId;
	private String date;
	private String money;
	private String sDay;
	private String eDay;
	private String pjCode;
	private String acCode;
	private String addr;
	private String port;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.meexpense_list_update);

		exp_date = (TextView) findViewById(R.id.textView_exp_update_date);
		exp_money = (TextView) findViewById(R.id.textView_exp_update_money);

		updateInfo();
		initButtons();// 버튼 세팅
		initSpinners();// 스니퍼 동적생성

	}

	private void updateInfo() {
		Intent receiveI = getIntent();
		boolean doYN = receiveI.getBooleanExtra("updating", false);
		if (doYN) {
			myId = receiveI.getStringExtra("myId");
			addr = receiveI.getStringExtra("loginAddr");
			port = receiveI.getStringExtra("loginPort");
			pjtList = new ArrayList<String[]>();
			acntList = new ArrayList<String[]>();
			String pCode = "-- 선택 --,";
			String pName = "-- 선택 --,";
			String aCode = "-- 선택 --,";
			String aName = "-- 선택 --,";

			String[] pjt_code = receiveI.getStringArrayExtra("projectCode");
			String[] pjt_name = receiveI.getStringArrayExtra("projectName");
			String[] acnt_code = receiveI.getStringArrayExtra("acntCode");
			String[] acnt_name = receiveI.getStringArrayExtra("acntName");

			for (int i = 0; i < pjt_code.length; i++) {
				pCode = pCode.concat(pjt_code[i] + ",");
				pName = pName.concat(pjt_name[i] + ",");
			}
			for (int i = 0; i < acnt_code.length; i++) {
				aCode = aCode.concat(acnt_code[i] + ",");
				aName = aName.concat(acnt_name[i] + ",");
			}

			String[] pjtCode = MEDataHandler.checkArray(pCode);
			String[] pjtName = MEDataHandler.checkArray(pName);
			String[] acntCode = MEDataHandler.checkArray(aCode);
			String[] acntName = MEDataHandler.checkArray(aName);

			pjtList.add(0, pjtCode);
			pjtList.add(1, pjtName);
			acntList.add(0, acntCode);
			acntList.add(1, acntName);

			date = receiveI.getStringExtra("date");
			money = receiveI.getStringExtra("money");
			exp_date.setText(date);
			exp_date.setGravity(Gravity.CENTER);
			exp_date.setTextSize(20);
			exp_date.setTextColor(Color.BLACK);

			exp_money.setText(MEDataHandler.addComma(money));
			exp_money.setGravity(Gravity.RIGHT);
			exp_money.setTextSize(20);
			exp_money.setTextColor(Color.BLACK);

			sDay = receiveI.getStringExtra("sDay");
			eDay = receiveI.getStringExtra("eDay");
			pjCode = receiveI.getStringExtra("pCode");
			acCode = receiveI.getStringExtra("aCode");// 수정대상
		}
	}

	private void initSpinners() {
		final String[] pjtSnipper = pjtList.get(1);
		final String[] acntSnipper = acntList.get(1);
		ArrayAdapter<String> adapter_pjt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, pjtSnipper);
		ArrayAdapter<String> adapter_acnt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, acntSnipper);

		Spinner sp_pjt = (Spinner) findViewById(R.id.spinner_update_pjt);
		adapter_pjt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_pjt.setAdapter(adapter_pjt);

		sp_pjt.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				selectedPjt = pjtSnipper[position];
				pjtCode = pjtList.get(0)[position];

			}

			public void onNothingSelected(AdapterView<?> parent) {
				selectedPjt = "-- 선택 --";
			}

		});

		Spinner sp_acnt = (Spinner) findViewById(R.id.spinner_update_acnt);
		adapter_acnt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_acnt.setAdapter(adapter_acnt);

		sp_acnt.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				selectedAcnt = acntSnipper[position];
				acntCode = acntList.get(0)[position];
			}

			public void onNothingSelected(AdapterView<?> parent) {
				selectedAcnt = "-- 선택 --";

			}
		});
	}

	private void initButtons() {
		btn_expense_update = (Button) findViewById(R.id.btn_expense_register); // 정산내역수정버튼

		btn_expense_update.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				if (selectedPjt.equals("-- 선택 --")) {
					new AlertDialog.Builder(MEExpenseListUpdate.this).setTitle("알림").setMessage("프로젝트를 선택하세요")
							.setPositiveButton("OK", null).show();
				} else if (selectedAcnt.equals("-- 선택 --")) {
					new AlertDialog.Builder(MEExpenseListUpdate.this).setTitle("알림").setMessage("계정을 선택하세요").setPositiveButton("OK", null)
							.show();
				} else {

					Intent expI = new Intent(MEExpenseListUpdate.this, SocketClientService.class);
					expI.putExtra("id", 48); // 48
					expI.putExtra("myId", myId); // 48
					expI.putExtra("sDate", sDay);
					expI.putExtra("eDate", eDay);
					expI.putExtra("searchPCode", pjCode);
					expI.putExtra("searchACode", acCode);// 수정조회대상
					expI.putExtra("pCode", pjtCode);
					expI.putExtra("aCode", acntCode);
					expI.putExtra("date", date);
					expI.putExtra("money", money);
					expI.putExtra("loginAddr", addr);
					expI.putExtra("loginPort", port);
					expI.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
					startService(expI);
					finish();
				}
			}
		});

	}
}
